local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.TaskBase());
local PetTool=_G.CommonOHOH.load.PetTool();

--任务内容: 获得某种宠物
-- cfg={
--     id="ugc_1_2"
--     finish=1,
--     condition={幼儿,虚空,传奇}
-- }
--判断两个宠物数据的差异
local function GetChangePetData  (old,now)
    local change={}
    local ischanged=false
    for k,v in pairs(now) do
        if not old[k] then
            change[k]=v
            ischanged=true
        elseif v-old[k]>0   then
            change[k]=v-old[k]
            ischanged=true
        end
    end
    if ischanged then
        return change
    else
        return nil
    end
   
end

--獲取稱號ID
local function GetTitleIdByConfgId(cfgid)
    return 1000
end

local function IsInConditon  (cfgId,levelId,evolutionId,condition)
   local tId=GetTitleIdByConfgId(cfgId)
   if levelId>=condition[1] and evolutionId>=condition[2]and tId>=condition[3] then
         return true
   end
   return false
end

-- 获取满足条件的宠物数量
local function GetPetInConditonCount  (petData,condition)
    local sum=0
    for k,v in pairs(petData) do
        local data= PetTool.GetPetIdInfo(k)
        if IsInConditon(data[1],data[2],data[3],condition) then
            sum=sum+v
        end
    end
    return sum
end


function M.new(...)
	return M(...);
end;

function M.ctor(self,player,finish,tId,...)--构造函数
    self.condition={...}
	M.super.ctor(self,player,finish,tId);
end;

function M.ClientInit(self,uiFuc)--客户端初始化
    M.super.ClientInit(self);
    self.OldHadPet= CommonOHOH:DeepCopy(self.DataBase:GetData("pets").had)
    if not self:IsComplete() then
        local con=self.Player.UserDataEvent:Connect(function(data)
            if data["pets"] then
                local now=data["pets"].had
                local change=  GetChangePetData(self.OldHadPet,now)
                if change then
                   local sum= GetPetInConditonCount(change, self.condition)
                   self.OldHadPet= CommonOHOH:DeepCopy(self.DataBase:GetData("pets").had)
                   self:UpdateProgress(sum)
                end
            end
        end)
        self:AddConnect(con)
    end
end

function M.ServerInit(self)--服务器初始化
    M.super.ServerInit(self);
    self.OldHadPet= CommonOHOH:DeepCopy(self.DataBase:GetData("pets").__real.had)
    if not self:IsComplete() then
        local con=self.Player.UserDataEvent:Connect(function(key)
            if key=="pets" then
                local now=self.DataBase:GetData("pets").__real.had
                local change=  GetChangePetData(self.OldHadPet,now)
                if change then
                   local sum= GetPetInConditonCount(change, self.condition)
                   self.OldHadPet= CommonOHOH:DeepCopy(self.DataBase:GetData("pets").__real.had)
                   self:UpdateProgress(sum)
                end
            end
        end)
        self:AddConnect(con)
    end

end

function M.dtor(self)--析构函数
    M.super.dtor(self);
end;

return M